package com.itmayiedu.controller;

import java.util.LinkedHashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.druid.util.StringUtils;
import com.itmayiedu.base.ResponseBase;
import com.itmayiedu.constants.Constants;
import com.itmayiedu.entity.UserEntity;
import com.itmayiedu.feign.MemberServiceFeign;
import com.itmayiedu.utils.CookieUtil;
import com.qq.connect.QQConnectException;
import com.qq.connect.api.OpenID;
import com.qq.connect.javabeans.AccessToken;
import com.qq.connect.oauth.Oauth;

@Controller
public class LoginController {
	@Autowired
	private MemberServiceFeign memberServiceFeign;
	private static final String LOGIN = "login";
	private static final String RELATION = "qqrelation";
	private static final String INDEX = "redirect:/";
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String loginGet() {
		return LOGIN;
	}
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String loginPost(UserEntity userEntity,HttpServletRequest request,HttpServletResponse response) {
		//1.验证参数
		//2调用登陆接口获取token信息
		ResponseBase loginBase = memberServiceFeign.login(userEntity);
		if(!loginBase.getCode().equals(Constants.HTTP_RES_CODE_200)) {
			request.setAttribute("error", "帐号或者密码错误");
			return LOGIN;
		}
		
		LinkedHashMap logindata=(LinkedHashMap) loginBase.getData();
		String membertoken = (String) logindata.get("memberToken");
		if(StringUtils.isEmpty(membertoken)) {
			request.setAttribute("error", "会话已经失效");
		}
		//3.将token信息存放在cookie中
		setCooike(membertoken, response);
		return INDEX;
	}
		//生成qq登陆链接
	@RequestMapping("locaQQLogin")
	public String locaQQLogin(HttpServletRequest request) throws QQConnectException {
		String authorizeURL = new Oauth().getAuthorizeURL(request);
		return "redirect:"+authorizeURL;
	}
	private void setCooike(String membertoken,HttpServletResponse response) {
		CookieUtil.addCookie(response, Constants.COOKIE_MEMBER_TOKEN, membertoken, Constants.COOIKE_MEMBER_TIME	);
	}
	@RequestMapping("/qqLoginCallback")
	public String qqLoginCallback(HttpServletRequest request, HttpServletResponse response, HttpSession httpSession)
			throws QQConnectException {
		//1.获取授权码
		//2.使用授权码获取accesstoken
		AccessToken accessTokenObj = new Oauth().getAccessTokenByRequest(request);
		if (accessTokenObj == null) {
			request.setAttribute("error", "qq授权失败!");
			return "error";
		}  
		String accessToken = accessTokenObj.getAccessToken();
		if (StringUtils.isEmpty(accessToken)) {
			request.setAttribute("error", "accessToken为空!");
			return "error";
		}
		// 获取openid
			OpenID openIdObj = new OpenID(accessToken);
			String userOpenID = openIdObj.getUserOpenID();
			ResponseBase openIdUser = memberServiceFeign.findByOpenIdUser(userOpenID);
			// 用戶沒有关联QQ账号
			if (openIdUser.getCode().equals(Constants.HTTP_RES_CODE_201)) {
				// 跳转到管理账号
					httpSession.setAttribute("qqOpenid", userOpenID);
				return RELATION;
			}
			// 如果用户关联账号 直接登录
			LinkedHashMap dataMap = (LinkedHashMap) openIdUser.getData();
			String membertoken = (String) dataMap.get("membertoken");
			setCooike(membertoken, response);
			return INDEX;
	}
	//qq授权关联页面 已有账号
	@RequestMapping(value = "/qqRelation", method = RequestMethod.POST)
	public String qqRelation(UserEntity userEntity,HttpServletRequest request,HttpServletResponse response,HttpSession httpSession) {
		//1.获取openid
		String qqOpenid = (String) httpSession.getAttribute("qqOpenid");
		if(StringUtils.isEmpty(qqOpenid)) {
			request.setAttribute("error", "没有获取到openid");
			return "error";
		}
	//2调用登陆接口获取token信息
		userEntity.setOpenid(qqOpenid);
		ResponseBase loginBase = memberServiceFeign.qqLogin(userEntity);
			if(!loginBase.getCode().equals(Constants.HTTP_RES_CODE_200)) {
				request.setAttribute("error", "帐号或者密码错误");
				return LOGIN;
			}
				
			LinkedHashMap logindata=(LinkedHashMap) loginBase.getData();
			String membertoken = (String) logindata.get("memberToken");
			if(StringUtils.isEmpty(membertoken)) {
				request.setAttribute("error", "会话已经失效");
			}
			//3.将token信息存放在cookie中
			setCooike(membertoken, response);
			return INDEX;
	}
}
